Skip to content

round on the whole remainder in the Dfp string constructor - #362

Merged
aherbert merged 1 commit into
apache:masterfrom
insaf021:dfp-string-constructor-round
Nov 29, 2026
Merged

aherbert merged 1 commit into
apache:masterfrom
insaf021:dfp-string-constructor-round

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

Follow-up to the string parsing issue found while reviewing #359.

Dfp(DfpField, String) keeps one decimal digit past the mantissa and rounds with it:

final int excp = round((striped[q] - '0') * 1000);

round() takes its argument as a fraction of the last radix digit out of 10000, so a single decimal digit can only ever be a multiple of 1000. Two things fall out of that:

  • a remainder of 5678 is passed as 5000, which ROUND_HALF_EVEN reads as an exact half and takes down. On a 4 radix digit field, newDfp("123456789012345678") gives 1.2345678901234e17 where the nearest value is 1.2345678901235e17.
  • every digit below the first one is dropped before round() sees it, so a tie is never broken and FLAG_INEXACT is never raised for them. new DfpField(20).newDfp("1.0000000000000000000000001") returns 1. with no flags set.

The parser copies at most mant.length * rsize + 1 digits, so the information needed to decide either case was being thrown away during the copy.

The fix keeps a whole radix digit of remainder instead of one decimal digit, which is exactly the precision round() works in, and records whether a non-zero digit was dropped below that. Anything below the four digits only matters as "the remainder is strictly greater", so it is tracked as a single sticky bit and used to nudge n off the two values round() tests exactly, zero and 5000. Digits dropped during the copy and digits still sitting in the parsed string are both covered. striped is now filled with '0' up front, because the rounding digits can sit past the last position the copy and the zero padding write.

Checked against BigDecimal.round(new MathContext(keep, HALF_EVEN)) over 80k generated values across 7 field sizes, biased towards 0, 5 and 9 so ties and carries come up: 7665 disagreed before, none after. A sweep of every field size from 1 to 45 digits, with integer, fractional, mixed and exponent forms from 1 digit up to past the mantissa, also agrees throughout.

testStringConstructorRounding in DfpTest pins the 16 to 19 digit bands with explicit expected strings, rounding up and down in each, both signs, exact halves going to even, a half broken by a digit inside the parsed string and by one far past it, the carry out of the mantissa, and the flags each case raises. It fails on master at the 18 digit case and passes with the fix.

mvn clean verify javadoc:javadoc is green on legacy-core and legacy, checkstyle reports no violations.

The parser kept a single digit past the mantissa and rounded with
round(digit * 1000). A dropped "5678" therefore looked like an exact
half and ROUND_HALF_EVEN took it down, and a non-zero digit further
down was lost entirely, so a tie was never broken and FLAG_INEXACT
was not raised. Keep a full radix digit of remainder, track whether
anything non-zero was dropped below it, and pass that to round().
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.22%. Comparing base (f554608) to head (e2e3fbd).
⚠️ Report is 427 commits behind head on master.

Files with missing lines Patch % Lines
.../org/apache/commons/math4/legacy/core/dfp/Dfp.java 94.73% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #362      +/-   ##
============================================
+ Coverage     86.54%   87.22%   +0.68%     
+ Complexity     9787       91    -9696     
============================================
  Files           532      499      -33     
  Lines         35516    33555    -1961     
  Branches       6194     5862     -332     
============================================
- Hits          30738    29270    -1468     
+ Misses         3518     3164     -354     
+ Partials       1260     1121     -139     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aherbert
aherbert merged commit 5503071 into apache:master Nov 29, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants